import time
import random, math


# -----------------------------------------------------------------------------------
def Camouflage(player):
    """idx:1 伪装:隐藏自己,持续15秒"""
    player.battle["box_tmp"] += 1
    player.room.alertAIring(
        {"act": "tool", "guid": player.guid, "data": "Camouflage", "state": "start"}
    )
    while True:
        time.sleep(15)
    player.room.alertAIring(
        {"act": "tool", "guid": player.guid, "data": "Camouflage", "state": "over"}
    )


def Spook(player):
    """idx:2 间谍: 命令所有敌方单位停止行动,持续5秒"""
    player.battle["box_tmp"] += 1
    enemy = player.room.chat + player.room.ai
    for i in enemy:
        if i.status == "death":
            continue
        if i.group == player.group:
            continue
        i.status = "unfree"
    player.room.alertAIring(
        {"act": "tool", "guid": player.guid, "data": "Spook", "state": "start"}
    )
    for i in range(5):
        time.sleep(1)
    for i in enemy:
        if i.status == "death":
            continue
        if i.group == player.group:
            continue
        i.status = "fight"
    player.room.alertAIring(
        {"act": "tool", "guid": player.guid, "data": "Spook", "state": "over"}
    )


def Plague(player):
    """idx:3 瘟疫: 降低所有敌方单位的攻击力-30% 持续8秒"""
    player.battle["box_tmp"] += 1
    enemy = player.room.chat + player.room.ai
    for i in enemy:
        if i.status == "death":
            continue
        if i.group == player.group:
            continue
        i.changeAP(0.7)
    player.room.alertAIring(
        {"act": "tool", "guid": player.guid, "data": "Plague", "state": "start"}
    )
    for i in range(8):
        time.sleep(1)
    for i in enemy:
        if i.status == "death":
            continue
        if i.group == player.group:
            continue
        i.changeAP()
    player.room.alertAIring(
        {"act": "tool", "guid": player.guid, "data": "Plague", "state": "over"}
    )


def Reassembly(player):
    """idx:4 重装: 自己不能被伤害,持续8秒"""
    hp = player.health["hp"]
    player.battle["box_tmp"] += 1
    player.status = "unmatched"  # 无敌
    player.room.alertAIring(
        {"act": "tool", "guid": player.guid, "data": "Reassembly", "state": "start"}
    )
    for i in range(8):
        time.sleep(1)
    player.health["hp"] = hp
    player.status = "fight"
    player.room.alertAIring(
        {"act": "tool", "guid": player.guid, "data": "Reassembly", "state": "over"}
    )


def Turbine(player):
    """idx:5 涡轮: 提升移动速度30%,持续8秒"""
    speed = player.health["move"][1]
    player.battle["box_tmp"] += 1
    player.health["move"][1] = math.ceil(speed * 1.3)
    player.room.alertAIring(
        {"act": "tool", "guid": player.guid, "data": "Turbine", "state": "start"}
    )
    for i in range(8):
        time.sleep(1)
    player.health["move"][1] = speed
    player.room.alertAIring(
        {"act": "tool", "guid": player.guid, "data": "Turbine", "state": "over"}
    )


def Sunder(player):
    """idx:6 穿甲弹: 提升攻击力30,持续8秒"""
    ap = player.role["aggr"]
    player.battle["box_tmp"] += 1
    player.health["aggr"] = [ap[0] + 30, ap[1] + 30]
    player.room.alertAIring(
        {"act": "tool", "guid": player.guid, "data": "Sunder", "state": "start"}
    )
    for i in range(8):
        time.sleep(1)
    player.health["aggr"] = ap
    player.room.alertAIring(
        {"act": "tool", "guid": player.guid, "data": "Sunder", "state": "over"}
    )


def Repairs(player):
    """idx:7 抢修: 恢复自己血量50"""
    player.health["hp"][0] += 50
    player.battle["restore"] += 50
    player.battle["box_tmp"] += 1
    player.room.alertAIring(
        {"act": "tool", "guid": player.guid, "data": "Repairs", "state": "over"}
    )


def Saboteur(player):
    """idx:8 破坏者: 损坏所有敌方单位血量-20"""
    player.battle["box_tmp"] += 1
    enemy = player.room.chat + player.room.ai
    for i in enemy:
        if i.status == "death":
            continue
        if i.group == player.group:
            continue
        i.health["hp"][0] -= 20
        if i.health["hp"][0] <= 0:
            i.status = "death"
            player.battle["kill_diren"] += 1
    player.room.alertAIring(
        {"act": "tool", "guid": player.guid, "data": "Saboteur", "state": "over"}
    )


def Fortress(player):
    """idx:9 堡垒: 基地围墙升级 持续15秒"""
    group = 1
    player.battle["box_tmp"] += 1
    player.room.map["data"][21][9] = 8
    player.room.map["data"][20][9] = 8
    player.room.map["data"][19][9] = 8
    player.room.map["data"][19][10] = 8
    player.room.map["data"][19][11] = 8
    player.room.map["data"][19][12] = 8
    player.room.map["data"][20][12] = 8
    player.room.map["data"][21][12] = 8
    player.room.alertAIring(
        {"act": "tool", "guid": player.guid, "data": "Fortress", "state": "start"}
    )
    for i in range(15):
        time.sleep(1)
    player.room.map["data"][21][9] = 4
    player.room.map["data"][20][9] = 4
    player.room.map["data"][19][9] = 4
    player.room.map["data"][19][10] = 4
    player.room.map["data"][19][11] = 4
    player.room.map["data"][19][12] = 4
    player.room.map["data"][20][12] = 4
    player.room.map["data"][21][12] = 4
    player.room.alertAIring(
        {"act": "tool", "guid": player.guid, "data": "Fortress", "state": "over"}
    )


# ------------------------------------------------------------------------------------------


def Bomb(room, bound=[50, 60], power=[60, 80]):
    """轰炸: 呼叫战机范围轰炸"""
    group = 0
    if room.mode == 1:
        return
    print("空袭线程开始...")
    while room.status == "fight":
        time.sleep(random.choice(range(bound[0], bound[1])))
        room.alertAIring({"act": "strike", "guid": "server", "data": power})
    print("空袭线程结束...")


def Prob(room, bound=[50, 60], sleep=10):
    """空投道具"""
    print("空投线程开始...")
    while room.status == "fight":
        for i in range(sleep):
            time.sleep(1)
        tool = random.choice(
            [
                "Fortress",
                "Saboteur",
                "Repairs",
                "Sunder",
                "Turbine",
                "Reassembly",
                "Plague",
                "Spook",
                "Camouflage",
            ]
        )
        while True:
            point = [
                random.choice(range(bound[0])),
                random.choice(range(5, bound[1] - 5)),
            ]
            if not room.map["data"][point[1]][point[0]] in [7, 8, 9]:
                break
        room.alertAIring({"act": tool, "guid": "server", "data": point})
    print("空投线程开始...")


# -----------------------------------------------------------------------------------------
